#!/bin/sh
# Build and run Home Assistant in Docker.

# Optional: pass in a timezone as first argument
# If not given will attempt to mount /etc/localtime

# Stop on errors
set -e

MACHINE=$(uname -m)
PWD=$(pwd)

cd "$(dirname "$0")/.."

# Should be amended with other platforms
case "$MACHINE" in
  armv7l) DOCKERFILE="DockerfileRPI" ;;
  *)      DOCKERFILE="Dockerfile" ;;
esac

docker build -t tradfri-dev -f "$PWD/$DOCKERFILE" .

if [ $# -gt 0 ]
then
  docker run \
    --net=host \
    -e "TZ=$1" \
    -v `pwd`:/usr/src/app \
    --rm \
    -t -i tradfri-dev bash
elif [ "$(uname)" = "Darwin" ] && [ $# -eq 0 ]
then
  OSX_TZ=$(ls -la /etc/localtime | cut -d/ -f8-9)
  docker run \
    --net=host \
    -e "TZ=$OSX_TZ" \
    -v `pwd`:/usr/src/app \
    --rm \
    -t -i tradfri-dev bash
else
  docker run \
    --net=host \
    -v /etc/localtime:/etc/localtime:ro \
    -v `pwd`:/usr/src/app \
    --rm \
    -t -i tradfri-dev bash
fi
